/**
 * Created by Administrator on 15-6-26.
 */
var Query = RichBase.extend({
	statics : {
		currentPage : 0,
		last : "",
		oData : {},
		queryState : {
			loading : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>',
			empty : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">没有产品...</p></li>',
			fail : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求失败...</p></li>',
			unlogin : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">登录过期，请重新登录...</p></li>',
			timeout : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">网络请求超时...</p></li>',
			serverError : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求出错...</p></li>'
		},
		getProv : function(id){
			var prov = "";
			var p = PROVINCES;
			if(!id) return prov;
			for(var i in p){
				var areaid = p[i]["area_id"];
				if(areaid==id){
					prov = p[i]["area_name"];
					break;
				}
			}
			return prov;
		},
		isObjEmpty : function(obj){
			for(var i in obj){
				return false;
			}
			return true;
		}
	},
	EVENTS : {
		"click" : {
			"#pagenavW .pageBtn" : "onPrevNextClick"
		}
	},
	init : function(opt){
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
	},
	setCurPage : function(page){
		this.statics.currentPage = page;
	},
	getCurPage : function(){
		return this.statics.currentPage;
	},
	getData : function(){
		return this.statics.oData;
	},
	setData : function(id,data){
		if(id && data){
			this.statics.oData[id] = data;
		}else{
			this.statics.oData = {};
		}
	},
	onPrevNextClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		if(tarBtn.hasClass("disable")) return false;
		var oData = that.statics.oData;
		var curPage = that.statics.currentPage;
		var toPage = tarBtn.hasClass("prevBtn") ? (curPage-1) : (curPage+1);
		if(tarBtn.hasClass("prevBtn")){
			that.nextBtn.removeClass("disable");
			that.statics.currentPage = toPage;
			that.buildHtml(oData[toPage]);
			if(!that.statics.oData[toPage-1]) that.prevBtn.addClass("disable");
		}else{
			that.prevBtn.removeClass("disable");
			if(oData[toPage]){
				that.statics.currentPage = toPage;
				that.buildHtml(oData[toPage]);
				if(!that.statics.oData[toPage+1] && !that.statics.last){
					that.nextBtn.addClass("disable");
				}
			}else{
				that.fire("query_next",oData[curPage]["last"]);
			}
		}
	},
	query : function(opt){
		var that = this;
		var opt = opt || {};
		var pageSize = opt.pageSize || 15;
		var last = opt.last || "";
		var provice = opt.provice || "";
		var city = opt.city || "";
		//var title = opt.title || "";
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data = {
			pageSize : pageSize,
			last : last,
			provice : provice,
			city : city,
			//title : title,
			async : 1
		};
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		PFT.Ajax({
			//url : "product_package.html",
			url : "prod_sale_dev.html",
			type : "GET",
			dataType : "json",
			ttimeout : ttimeout,
			data : data,
			loading : function(){
				listUl.html(status["loading"]);
				pagenavW.hide();
			},
			removeLoading : function(res){
				that.statics.last = "";
				that.statics.currentPage += 1;
				listUl.html("");
				pagenavW.show();
			},
			timeout : function(){ listUl.html(status["timeout"]);},
			serverError : function(){ listUl.html(status["serverError"]);}
		},function(res){
			var code = res.code;
			var lists = res.lists;
			var last = res.last;
			if(code==200){
				if(!that.statics.isObjEmpty(lists)){
					that.statics.oData[that.statics.currentPage] = res;
					that.buildHtml(res);
				}else{
					listUl.html(status["empty"]);
					if(provice) that.fire("search_empty",res);
				}
				that.statics.last = last;
				if(!last){
					that.fire("no_next",res);
				}else{
					that.fire("has_next",res);
				}
			}else if(code==0){
				listUl.html(status["unlogin"]);
			}else{
				listUl.html(status["fail"]);
			}
		})
	},
	buildHtml : function(res){
		var that = this;
		var code = res.code;
		var lists = res.lists;
		var str = "";
		var last = res.last;
		var item_a = $("#itemTpl").html();
		var item_div = $("#itemTpl_tr").html();
		var item_con = $("#itemTpl_con").html();
		var result = "";
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		lists = sData.adapt(lists);
		var data = lists;
		for(var i in data){
			is_data_empty = false;
			str += '<div class="list">';
			str += '<div class="list_top position_a ">';
			str +=     '<div class="title" data="'+data[i]["title"]+'">'+data[i]["title"]+'</div>';
			str += 			'<div class="top_right_a">';
			str +=				'<span class="supplier">供应商</span>';
			str +=				'<span class="supply_price">供货价</span>';
			str +=				'<span class="retail_price">零售价</span>';
			str +=				'<span class="operate">操作</span>';
			str +=			'</div>';
			str +=       '</div>';
			for(var s in data[i]["ticket"]){
				var t = data[i]["ticket"][s];
				var lists =data[i]["ticket"][s]["lists"];
				var tt = data[i]["ticket"][s]["ttitle"];
				var pid = data[i]["ticket"][s]["pid"];
				var tid = t["pid"];
				str +=     '<div class="list_con" data-ac="">';
				str +=     '<div class="ticket"><span class="title">'+tt+'</span>ID:'+s+'</div>';
				for(var j in lists){
					var dname=lists[j]["dname"];
					var js=lists[j]["js"];
					var ls=lists[j]["ls"];
					var id=lists[j]["aid"];
					var active=lists[j]["active"];
					str +=     '<ul class="list_data">';
					str +=     '<li><span class="supplier">'+dname+'</span><span class="supply_price">'+js+'</span><span class="retail_price">'+ls+'</span><span class="operate_a color_a" onclick="window.open(\'http://www.12301.cc/set_price.html&pid='+pid+'&aid='+id+'\')">分销价格</span><span class="yujbtn" id="yuj" data-ac="" data-active="'+active+'" data-pid="'+pid+' "data-id="'+id+'"></span></li></ul>';
				}
				str +='</div>';
			}
			str += '</div>';
		}
		$("#mlistUl").html(str);
		// for(var i in lists){
			// var item = lists[i];
			// var title = lists[i];
			// var tickets = item.ticket;
			// //var area = item.area;
			// //console.log(tickets);
			// var ticketHtml = "";
			// var item_all = "";
			// var result = "";
			// var lists_div = "";
			// //var lists_con = "";
			// var itema = "";
			// var itemb = "";
			// //console.log(itemHtml)
			// for(var s in tickets){
				// //var tic = tickets[s];
				// var lists_ui =tickets[s]["lists"];
				// var lists_con = "";
				// //tic["ppid"] = tic["pid"];
				// //tic["lid_pid"] = s;
				// //item["lid_pid"] = i;
				// //title["title"] = item["title"];
				// var title_li = item["title"];
				// //tic["landname"] = tic["title"];
				// //var tictitle = tic["title"];
				// //lists_div += that.parseTemplate(item_div,tic);
				// //item["ticket_con"] = lists_div;
				// for(var j in lists_ui){
					// var lists_li = lists_ui[j];
					// lists_li["activea"] = lists_li["active"];
					// lists_li["aida"] = lists_li["aid"];
					// lists_li["dnamea"] = lists_li["dname"];
					// lists_li["jsa"] = lists_li["js"];
					// lists_li["lsa"] = lists_li["ls"];
					// lists_li["lvla"] = lists_li["lvl"];
					// lists_con += that.parseTemplate(item_con,lists_li);
					// //console.log(lists_li["ls"]);
				// }
				// //itema["ticket_con"] = lists_con;
				// //itema["landname"] = tictitle;
				// //itema["lid_pid"] = i;
				// //result += that.parseTemplate(item_div,itema);
				// //item_all += that.parseTemplate(item_a,title);
			// }
			// //itemb["tickets"] = result;
			// //itemb["title"] = title_li;
			// //build_div += that.parseTemplate(item_a,itemb);
		// }
		 //listUl.html(build_div);
		 //listUl.html(lists_con);
		//$("#whichPageNum").text(that.statics.currentPage);
	}
});





